package com.crazy.crypto.digest;

import java.security.MessageDigest;

import org.apache.commons.codec.digest.DigestUtils;

public class SHAUtil {

	public static String sha1(String src) {
		return DigestUtils.sha1Hex(src).toUpperCase();
	}
	
	public static String sha1b(String src) {
		String result = null;
		try {
	        MessageDigest digest = java.security.MessageDigest
	                .getInstance("SHA-1");
	        digest.update(src.getBytes("UTF-8"));
	        byte messageDigest[] = digest.digest();
	        StringBuffer hexString = new StringBuffer();
	        for (int i = 0; i < messageDigest.length; i++) {
	            String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
	            if (shaHex.length() < 2) {
	                hexString.append(0);
	            }
	            hexString.append(shaHex);
	        }
	        result = hexString.toString().toUpperCase();

	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return result;
	}
	
	public static void main(String[] args) {
		String src = "hello world";
		System.out.println(sha1(src));
		System.out.println(sha1b(src));
	}
}
